﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using spbtzb;

public partial class download : System.Web.UI.Page
{
    protected string strConnection = spbtzb.ConString.ConnString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request["FileUrl"] != null && Request["ID"] != null && Request["vt"] != null)
            {
                Int32 iVtime = Convert.ToInt32(Request["vt"]) + 1;
                Int32 iret = AccessHelper.ExecuteNonQuery(strConnection, CommandType.Text,
                 "UPDATE FileList SET ViewTimes = " + iVtime + " WHERE ID = "
                 + Convert.ToInt32(Request["ID"]));

                string strFilePath2 = Server.MapPath("~/filesUpload/") + Request["FileUrl"].ToString();
                FileInfo fi = new FileInfo(strFilePath2);
                //判断文件是否存在
                if (fi.Exists)
                {

                    //将文件保存到本机上
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
                    Response.AddHeader("Content-Length", fi.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.Filter.Close();
                    Response.WriteFile(fi.FullName);
                    Response.End();
                }
                else
                {
                    //Page.RegisterClientScriptBlock("tip", "<script>alert('该文件不存在，请重新选择或联系管理员！')</script>");
                    Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "该文件不存在");
                }

            }
        }// !postback
    }
}
